package com.gw.vip.server.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gw.vip.server.po.BsOrder;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author GW
 * @description
 * @since 2022-04-28
 */
public interface BsOrderMapper extends BaseMapper<BsOrder> {

    /**
     * 根据店铺号和客户订单号获取订单
     * @param shopId
     * @param clientOrder
     * @return
     */
    @Select("SELECT * FROM bs_order WHERE shop_id=#{shopId} AND client_order=#{clientOrder}")
    BsOrder getByBase(String shopId, String clientOrder);

    /**
     * 获取清算日内初始交易的数量
     * @param clearMaxTime
     * @return
     */
    @Select("SELECT COUNT(*) FROM bs_order WHERE crt_time < #{clearMaxTime} AND status='O'")
    Integer getInOriCount(LocalDateTime clearMaxTime);

    /**
     * bs_order 设置清算日期-立即设置清算日期的交易
     * @param clearDate
     * @param clearMaxTime
     */
    @Update("UPDATE bs_order SET clear_date = #{clearDate} WHERE complete_time < #{clearMaxTime} AND status!='T' AND order_type IN (SELECT ID FROM sys_order_type WHERE clear_flag='1')")
    void setClearDate(LocalDate clearDate, LocalDateTime clearMaxTime);

    /**
     * 根据清算日期查找锁定类交易
     * @param clearDate
     * @return
     */
    @Select("SELECT * FROM bs_order WHERE clear_date=#{clearDate} AND txn_type IN (SELECT ID FROM sys_txn_type WHERE trans_lock_flag='1')")
    List<BsOrder> lockListByClearDate(LocalDate clearDate);
}
